<template>
    <TopNavComponent />
    <van-dropdown-menu ref="menuRef">
        <van-dropdown-item title="位置" ref="location">
            <div class="list" :active="active" @click="onLocation">
                <h3 :class="[active == 'area' ? 'active' : '']" value="area">区域</h3>
                <h3 :class="[active == 'subway' ? 'active' : '']" value="subway">地铁</h3>
            </div>
            <div class="item">
                <van-cell-group v-if="active == 'area'">
                    <van-cell value="不限" @click="onItem('不限')" />
                    <van-cell value="东城区" @click="onItem('东城区')" />
                    <van-cell value="西城区" @click="onItem('西城区')" />
                    <van-cell value="朝阳区" @click="onItem('朝阳区')" />
                    <van-cell value="通州区" @click="onItem('通州区')" />
                </van-cell-group>
                <van-cell-group v-if="active == 'subway'">
                    <van-cell value="1号线" @click="onItem('1号线')" />
                    <van-cell value="2号线" @click="onItem('2号线')" />
                    <van-cell value="3号线" @click="onItem('3号线')" />
                    <van-cell value="4号线" @click="onItem('4号线')" />
                    <van-cell value="5号线" @click="onItem('5号线')" />
                </van-cell-group>
            </div>
        </van-dropdown-item>
        <van-dropdown-item title="租金" ref="price">
            <van-cell-group>
                <van-cell value="不限" @click="onPrice('不限')" />
                <van-cell value="2000元以下" @click="onPrice('2000')" />
                <van-cell value="2000-2500元" @click="onPrice('2500')" />
                <van-cell value="2500-3000元" @click="onPrice('3000')" />
            </van-cell-group>
        </van-dropdown-item>
        <van-dropdown-item title="类型" ref="type">
            <div class="container">
                <div class="title">商铺类型</div>
                <div class="btn" @click="onType('不限')">不限</div>
                <div class="btn" @click="onType('商场/超市')">商场/超市</div>
                <div class="btn" @click="onType('商业街')">商业街</div>
            </div>
        </van-dropdown-item>
        <van-dropdown-item title="更多" ref="more">
            <div class="container">
                <div class="title">面积</div>
                <div :class="[activeBtn1 == 'no' ? 'activeBtn' : '']" class="btn" @click="onMoreSize('no')">不限</div>
                <div :class="[activeBtn1 == '20' ? 'activeBtn' : '']" class="btn" @click="onMoreSize('20')">20m以下</div>
                <div :class="[activeBtn1 == '50' ? 'activeBtn' : '']" class="btn" @click="onMoreSize('50')">20~50m</div>
            </div>
            <div class="container">
                <div class="title">楼层</div>
                <div :class="[activeBtn2 == 'no' ? 'activeBtn' : '']" class="btn" @click="onMoreFloor('no')">不限</div>
                <div :class="[activeBtn2 == 'l0' ? 'activeBtn' : '']" class="btn" @click="onMoreFloor('l0')">地下</div>
                <div :class="[activeBtn2 == 'l1' ? 'activeBtn' : '']" class="btn" @click="onMoreFloor('l1')">一层</div>
                <div :class="[activeBtn2 == 'l2' ? 'activeBtn' : '']" class="btn" @click="onMoreFloor('l2')">二层</div>
            </div>
            <div class="container btnL">
                <div class="clearBtn" @click="onClearInfo">清空筛选</div>
                <div class="sureBtn" @click="onSure">确定</div>
            </div>
        </van-dropdown-item>
    </van-dropdown-menu>
    <div class="content">
        <van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
            <div class="place-item" v-for="(item, index) in list" :key="index" @click="onPlaceItem(item.id)">
                <img :src="item.image" alt="">
                <div class="place-item-desc">
                    <p>{{ item.title }}</p>
                    <span class="descs">{{ item.descs }}</span>
                    <span class="price">{{ item.price }}元/月</span>
                </div>
            </div>
        </van-list>
    </div>
</template>
<script setup lang="ts">

import { ref,reactive } from "vue"
import { useRouter } from "vue-router"
import api from "../../api/index"

import TopNavComponent from '@/components/TopNavComponent.vue'

const router = useRouter()

const menuRef = ref(null)
const location = ref<any>(null);
const price = ref<any>(null)
const type = ref<any>(null)
const more = ref<any>(null)
const active = ref<string>('area')
const activeBtn1 = ref<string>("")
const activeBtn2 = ref<string>("")

interface IList{
    id:number,
    image:any,
    title:string,
    descs:string,
    price:string
}

interface IParams{
    location:string,
    price:string,
    type:string,
    more:string[]
}

const list = ref<IList[]>([]);
const page = ref<number>(1)
const loading = ref(false);
const finished = ref(false);

const params:IParams = reactive({
    location:"不限",
    price:"不限",
    type:"不限",
    more:["不限","不限"]
})

const onLocation = (event: any) => {
    active.value = event.target.getAttribute("value")
}

const onItem = (value: string) => {
    location.value.toggle();
    params.location = value
}

const onPrice = (value: string) => {
    price.value.toggle();
    params.price = value
}

const onType = (value:string) =>{
    type.value.toggle();
    params.type = value
}

const onMoreSize = (value:string) =>{
    activeBtn1.value = value
    console.log(value)
}

const onMoreFloor = (value:string) =>{
    activeBtn2.value = value
    params.more.push(value)
}

const onClearInfo = () =>{
    activeBtn1.value=""
    activeBtn2.value=""
}

const onSure = () =>{
    more.value.toggle();
    console.log(params);
    // 重新发送网络请求获取数据,根据选项筛选内容
    http()
}

const onLoad = () => {
    http()
};

const http = () =>{
    api.getPlace({ 
        page: page.value,
        location:params.location,
        price:params.price
    }).then(res => {
        if (res.data.status == 200) {
            page.value++
            list.value = list.value.concat(res.data.data.result)
        }
        // 加载状态结束
        loading.value = false;
        // 数据全部加载完成
        if (res.data.status == 500) {
            finished.value = true;
        }
    })
}

const onPlaceItem = (id:number) =>{
    router.push({
        name:"placedetails",
        params:{id}
    })
}

</script>
<style lang="less" scoped>
.list {
    width: 50%;
    float: left;
    background: #f3f4f5;

    h3 {
        width: 100%;
        font-size: 14px;
        font-weight: 700;
        height: 40px;
        line-height: 40px;
        padding-left: 20px;
        box-sizing: border-box;
    }

    h3.active {
        background: #fff;
    }
}

.item {
    float: left;
    width: 50%;
}

.container {
    background: #fff;
    overflow: hidden;
    clear: both;
    .title {
        padding: 20px;
        box-sizing: border-box;
        font-size: 14px;
    }

    .btn {
        float: left;
        width: 80px;
        height: 30px;
        line-height: 30px;
        background: #f3f4f5;
        margin: 10px;
        text-align: center;
        font-size: 14px;
        margin-top: 0px;
        border-radius: 5px;
    }
    .clearBtn{
        width: 30%;
        height: 40px;
        line-height: 40px;
        text-align: center;
        background: #f3f4f5;
        border-radius: 5px;
        margin: 10px;
    }
    .sureBtn{
        width: 50%;
        height: 40px;
        line-height: 40px;
        text-align: center;
        background: #684886;
        color: #fff;
        border-radius: 5px;
        margin: 10px;
    }
}

.btnL{
    display: flex;
    justify-content: center;
}

.activeBtn{
    background-color: #684886 !important;
    color: #fff;
}

.content{
    padding: 10px;
    background: #fff;
    .place-item{
        border-bottom: 1px solid #f3f4f5;
        padding: 10px 0;
        display: flex;
        img{
            width: 150px;
            border-radius: 5px;
        }
        .place-item-desc{
            display: flex;
            flex-direction: column;
            justify-content: center;
            padding-left: 10px;
            p{
                font-size: 14px;
                margin: 5px 0;
            }
            .descs{
                display: block;
                font-size: 14px;
                color: #999;
                margin: 5px 0;
            }
            .price{
                display: block;
                font-size: 14px;
                color: #FF4444;
                margin: 5px 0;
            }
        }
    }
}
</style>